﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;


namespace Docentric.Examples.Browser
{
	public class RenderingCollectionArbitraryContentExample : ExampleBase
	{
		// Constructor
		public RenderingCollectionArbitraryContentExample()
		{
			Name = "RenderingCollectionArbitraryContent";
			Title = "Rendering Collection AC";
            Description = "'Rendering Collection Arbitrary Content' example shows how to use the List element to render collection of 'Product' objects. Products will this time not be listed in the table. Instead we will write each item (Product) on its own page. To achive this a page break was added to the end of the List element.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Get all products from the database.
			IEnumerable<Product> products = DataAccess.GetProducts();

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(products);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
